﻿#include "SystemConfig.h"
#include "../util/FileUtil.h"
#include "../util/SystemHelper.h"

SystemConfig::SystemConfig(void)
{
}

SystemConfig::~SystemConfig(void)
{
}

bool SystemConfig::LoadConfig()
{
	// 获取系统配置
	m_config_file = FileUtil::GetAppPath("config.xml");

	// 载入已有配置
	if (boost::filesystem::exists(m_config_file))
	{
		//std::locale::global(std::locale("chs"));
		boost::property_tree::ptree pt;
		boost::property_tree::read_xml(m_config_file, pt);

		// 本地配置
		m_local_config.SetId(pt.get<std::string>("config.local.id", ""));
		m_local_config.SetUrl(pt.get<std::string>("config.local.url", ""));
		m_local_config.SetUser(pt.get<std::string>("config.local.user", ""));
		m_local_config.SetPassword(pt.get<std::string>("config.local.password", ""));
		m_local_config.SetPreview(pt.get<bool>("config.local.preview", true));
		m_local_config.SetAuthUrl(pt.get<std::string>("config.local.auth", ""));
		m_local_config.SetHttpIp(pt.get<std::string>("config.local.http.ip", SystemHelper::get_mutable_instance().GetLocalIp()));
		m_local_config.SetHttpPort(pt.get<int>("config.local.http.port", 5700));
		m_local_config.SetUseSSL(pt.get<bool>("config.local.http.ssl", false));

		// 数据库配置
		m_db_config.SetDbType(pt.get<int>("config.db.type", 1));
		m_db_config.SetIp(pt.get<std::string>("config.db.ip", "127.0.0.1"));
		m_db_config.SetPort(pt.get<int>("config.db.port", 3306));
		m_db_config.SetDbName(pt.get<std::string>("config.db.name", "box3"));
		m_db_config.SetUser(pt.get<std::string>("config.db.user", "root"));
		m_db_config.SetPassword(pt.get<std::string>("config.db.password", "root"));
	}
	else
	{
		SaveConfig();
	}

	return true;
}

void SystemConfig::SaveConfig()
{
	try
	{
		boost::property_tree::ptree pt;
		pt.put("config.local.id", m_local_config.GetId());
		pt.put("config.local.url", m_local_config.GetUser());
		pt.put("config.local.user", m_local_config.GetUser());
		pt.put("config.local.password", m_local_config.GetPassword());
		pt.put("config.local.preview", m_local_config.GetPreview());
		pt.put("config.local.auth", m_local_config.GetAuthUrl());
		pt.put("config.local.http.ip", m_local_config.GetHttpIp());
		pt.put("config.local.http.port", m_local_config.GetHttpPort());
		pt.put("config.local.http.ssl", m_local_config.GetUseSSL());

		pt.put("config.db.type", m_db_config.GetDbType());
		pt.put("config.db.ip", m_db_config.GetIp());
		pt.put("config.db.port", m_db_config.GetPort());
		pt.put("config.db.name", m_db_config.GetDbName());
		pt.put("config.db.user", m_db_config.GetUser());
		pt.put("config.db.password", m_db_config.GetPassword());

		//std::locale::global(std::locale("chs"));
		boost::property_tree::xml_writer_settings<std::string> settings('\t', 1);
		boost::property_tree::write_xml(m_config_file.c_str(), pt, std::locale(), settings);
	}
	catch (boost::property_tree::ptree_error e)
	{
		printf("save config file error:%s", m_config_file.c_str());
	}
}

LocalConf& SystemConfig::GetLocalConf()
{
	return m_local_config;
}

DBConf& SystemConfig::GetDBConf()
{
	return m_db_config;
}

BoxConf& SystemConfig::GetBoxConf()
{
	return m_box_config;
}